from django.shortcuts import render,redirect
from django.contrib.auth.hashers import make_password, check_password
from django.http import HttpResponse,JsonResponse
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator
from myadmin import models
import time,os
from shop.settings import BASE_DIR
from django.core.paginator import Paginator

# Create your views here.
#个人中心首页
def personal(request):
    uid = request.session['userinfo']['uid']
    uorder = models.Order.objects.filter(uid=uid)
    goods = models.Goods.objects.all().values()

    # 实例化分页对象 只显示前两个订单
    p = Paginator(uorder, 2)
    page = int(request.GET.get('p',1))
    page1 = p.page(page)
    uid = request.session['userinfo']['uid']
    uinfo = models.Users.objects.filter(id=uid).values()

    return render(request,'myhome/person/index.html',{'uorder':page1,'page':page,'goods':goods,'uinfo':uinfo})

#个人信息
def information(request):

    uid = request.session['userinfo']['uid']
    uinfo = models.Users.objects.filter(id=uid).values()
    print('*************')
    return render(request,'myhome/person/information.html',{'uinfo':uinfo})

# 修改用户信息
def information(request):
    # 接受用户的id
    uid = request.session['userinfo']['uid']
    # 判断是get还是post
    if request.method=='GET':
        uinfo = models.Users.objects.filter(id=uid).values()
        
        return render(request,'myhome/person/information.html',{'uinfo':uinfo})
    elif request.method=='POST':
        
        # 获取用户的提交信息
        userinfo = request.POST.dict()
        # 更新数据
        uinfo = models.Users.objects.get(id=uid)
        uinfo.phone=userinfo['phone']
        uinfo.sex=userinfo['sex']
        uinfo.email=userinfo['email']
        # /static/pics/1548059841.0424712.png
        # 判断头像有没有上传
        file = request.FILES.get('head_url')
        if file:
            # 有头像上传，要把旧头像删除
            # /dajngo07/py17/shop    ./static/pics/1547686265.6370358.png
            os.remove('.'+uinfo.head_url)
            headurl=upload(file)
            uinfo.head_url=headurl

        uinfo.save()
        return HttpResponse('<script>alert("修改成功");location.href="'+reverse('myhome_personal')+'"</script>')

# 订单相关
def order(request):
    uid = request.session['userinfo']['uid']
    uorder = models.Order.objects.filter(uid=uid)
    goods = models.Goods.objects.all().values()

    return render(request,'myhome/person/order.html',{'goods':goods,'uorder':uorder})
#删除
def delorder(request):
    try:
        rid = request.GET.get('rid')

        uod = models.Order.objects.get(id=rid)
        
        uod.delete()

        return JsonResponse({'error':1,'msg':'删除成功！'})
    except:
        return JsonResponse({'error':0,'msg':'删除失败！'})

#收货地址
def address(request):
    uid = request.session['userinfo']['uid']
    ures = models.Address.objects.filter(uid=uid)

    return render(request,'myhome/person/address.html',{'res':ures})

        














def upload(myfile):
    
    # 新名字
    filename = str(time.time())+"."+myfile.name.split('.').pop()
    destination = open("./static/pics/"+filename,"wb+")
    for chunk in myfile.chunks():      # 分块写入文件  
        destination.write(chunk)  
    destination.close()
    # /static/pics/filename
    return '/static/pics/'+filename